package com.sw.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sw.model.Module;
import com.sw.service.ModuleService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/module")
public class ModuleController {

	@Autowired
	private ModuleService moduleService;
	
	/**
	 * 添加
	 * @param module
	 * @return
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
	public @ResponseBody String addModule(Module module){
		boolean result = moduleService.insertModule(module);
		JSONObject jsonObject = new JSONObject();  
		if(result){
		    jsonObject.put("msg", "success");  
		}else{
			jsonObject.put("msg", "failure");  
		}
		 
	     return jsonObject.toString();
	}
	
	/**
	 * 修改
	 * @param module
	 * @return
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	public @ResponseBody Object updateModule(@RequestBody Module module){
		 boolean result = moduleService.updateModule(module);
		 JSONObject jsonObject = new JSONObject();  
		 if(result){
			 jsonObject.put("msg", "success");  
		 }else{
			 jsonObject.put("msg", "failure");  
		 }
	     return jsonObject.toString();  
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/selectById/{id}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public @ResponseBody String selectModule(@PathVariable("id") int id){
		System.out.println(id);
		Module module = moduleService.selectModuleById(id);
		JSONObject jsonObject = JSONObject.fromObject(module);
		return jsonObject.toString();
	}
	
	/**
	 * 根据名称查询
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/selectByName/{name}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public @ResponseBody String selectModuleByName(@PathVariable("name") String name){
		Module module = moduleService.selectModuleByName(name);
		JSONObject jsonObject = JSONObject.fromObject(module);
		return jsonObject.toString();
	}
	
	/**
	 * 查询全部
	 * @return
	 */
	@RequestMapping(value="/selectModuleAll",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	public @ResponseBody String selectModuleAll(){
		List<Module> list = moduleService.getModuleAll();
		JSONObject jsonObject;
		JSONArray jsonArray = new JSONArray();
		for (Module module : list) {
			jsonObject = JSONObject.fromObject(module);
			jsonArray.add(jsonObject);
		}
		
		return jsonArray.toString();
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
	public @ResponseBody Object deleteModule(@PathVariable("id") int id){
		boolean result = moduleService.deleteModuleById(id);
		JSONObject jsonObject = new JSONObject();  
		if(result){
			jsonObject.put("msg", "success");  
		}else{
			jsonObject.put("msg", "failure");  
		}
        
        return jsonObject;  
	}
}
